package com.blb.zy_community.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.zy_community.entity.*;
import com.blb.zy_community.mapper.ZyOwnerRoomMapper;
import com.blb.zy_community.mapper.ZyOwnerRoomRecordMapper;
import com.blb.zy_community.service.ISysUserService;
import com.blb.zy_community.service.IZyOwnerRoomRecordService;
import com.blb.zy_community.service.IZyOwnerRoomService;
import com.blb.zy_community.service.IZyOwnerService;
import com.blb.zy_community.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 房屋绑定记录表  前端控制器
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@RestController
@CrossOrigin
public class ZyOwnerRoomRecordController {

    @Autowired
    private IZyOwnerRoomRecordService zyOwnerRoomRecordService;
    @Autowired
    private IZyOwnerService zyOwnerService;

    @Autowired
    private ZyOwnerRoomMapper zyOwnerRoomMapper;
    @Autowired
    private IZyOwnerRoomService zyOwnerRoomService;
    @Autowired
    private ISysUserService sysUserService;
    @PutMapping("ownerRoomRecord/{recordAuditOpinion}/{username}/{type}")
    public ResponseResult<String> updateOwnerRoomRecord(@RequestBody ZyOwnerRoom zyOwnerRoom,@PathVariable String recordAuditOpinion, @PathVariable String username, @PathVariable String type){
        try {
            ZyOwnerRoomRecord orm = new ZyOwnerRoomRecord();
            ZyOwnerRoom ownerRoom = zyOwnerRoomService.getOne(new QueryWrapper<ZyOwnerRoom>().eq("room_id",zyOwnerRoom.getRoomId()).eq("room_status","Binding"));

            if(ownerRoom != null){
                throw new Exception("该房屋已绑定业主");
            }
//        List<ZyOwnerRoom> ownerRooms = zyOwnerRoomMapper.selectList(new QueryWrapper<>(new ZyOwnerRoom() {
//            {
//                setRoomId(ownerRoom.getRoomId());
//                setRoomStatus("Binding");
//            }
//        }));
            long recordId = new Long((long) ((Math.random() + 1) * Math.pow(10, 19 - 1))).longValue();
            //房屋绑定记录id
            orm.setRecordId(recordId);
            System.out.println(recordId);
            //房屋绑定id
            orm.setOwnerRoomId(String.valueOf(zyOwnerRoom.getOwnerRoomId()));
            //小区id
            orm.setCommunityId(zyOwnerRoom.getCommunityId());
            //楼栋id
            orm.setBuildingId(zyOwnerRoom.getBuildingId());
            //单元id
            orm.setUnitId(zyOwnerRoom.getUnitId());
            //房间id vx
            orm.setRoomId(zyOwnerRoom.getRoomId());
            //业主id
            orm.setOwnerId(zyOwnerRoom.getOwnerId());
            //业主类型
            orm.setOwnerType(zyOwnerRoom.getOwnerType());
            //绑定状态
            orm.setRoomStatus(type);
            //审核意见
            orm.setRecordAuditOpinion(recordAuditOpinion.toString());
            //审核人类型
            orm.setRecordAuditType("web");
            //创建人id
            SysUser user = sysUserService.getOne(new QueryWrapper<SysUser>().eq("user_name", username));
            orm.setCreateById(user.getUserId());
            //创建者
            orm.setCreateName(user.getUserName());
            //创建时间
            orm.setCreateTime(LocalDateTime.now());
            zyOwnerRoom.setRoomStatus(orm.getRoomStatus());
            zyOwnerRoomService.updateOwnerRoom(zyOwnerRoom);
            zyOwnerRoomRecordService.save(orm);
            return ResponseResult.ok("ok");
        } catch (Exception e) {
            return ResponseResult.error(500L,e.getMessage());
        }
    }

    @PutMapping("turnDownOwnerRoomRecord")
    public ResponseResult<String> turnDownOwnerRoomRecord(@RequestBody Map<String,Object> param){
        Object recordId = param.get("recordId");
        Object recordAuditOpinion = param.get("recordAuditOpinion");
        zyOwnerRoomRecordService.turnDownRecord((String) recordId, (String) recordAuditOpinion);
        return ResponseResult.ok(param);
    }

}
